<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\CustomDevPlanSaveRequest;
use App\Models\CustomDevPlan;
use App\Http\ErrCode;
use Auth;
use Excel;

class CustomDevPlanController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $sale_intentin_id = $request->get('sale_intention_id',0);
        $list = CustomDevPlan::where(['sale_intention_id'=>$sale_intentin_id])->get();
        return response()->json(json_success($list));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CustomDevPlanSaveRequest $request)
    {
        $data = array_null_to_empty($request->only(['sale_intention_id','plan_date','plan_content']));

        $model = new CustomDevPlan();
        $model->fill($data);
        $model->save();
        return response()->json(json_success(['id'=>$model->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = CustomDevPlan::with('saleIntention')->findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CustomDevPlanSaveRequest $request, $id)
    {
        $data = array_filter($request->only(['plan_date','plan_content','result']));

        $model = CustomDevPlan::findOrFail($id);
        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = CustomDevPlan::findOrFail($id);
        $model->delete();
        return response()->json(json_success());
    }
}
